<template>
  <el-dialog title="编辑弹层" :visible="visible" :before-close="handleClose">
    <el-form label-width="120px" :model="roleForm" :rules="rules">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="roleForm.name" /></el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input v-model="roleForm.description" /></el-form-item>
    </el-form>
    <!-- 底部 -->
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" @click="handleClose">取消</el-button>
        <el-button size="small" type="primary" @click="submit">确定</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { updateRole, addRoleList } from '@/api/setting'
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    roleForm: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 确定 修改编辑和 增加角色
    async submit() {
      if (this.roleForm.id) {
        // 编辑
        const res = await updateRole(this.roleForm)
        console.log(res)
      } else {
        // 新增
        const res = await addRoleList(this.roleForm)
        console.log(res)
      }
      this.$parent.getRoleList()
      this.$parent.handelClose()
      this.$message.success('添加成功')
    },

    // 关闭弹框
    handleClose() {
      this.$parent.handelClose()
    }
  }
}
</script>

<style>

</style>
